function Monitor() {
}

Monitor.inherit(Control);

Monitor.prototype.init = function() {
    Monitor.base.init.call(this);
}
/**
 * 创建可视对讲控件
 */
Monitor.prototype.create = function() {
    this._id = this.getAttribute().id;
    var _html = '<div class="monitor-body">';
    _html += '      <div class="top">';
    _html += '          <div class="title"></div>';
    _html += '          <div class="opt-btn">';
    _html += '              <span class="full" title="全屏"></span>';
    _html += '              <span class="nofull" title="关闭全屏"></span>';
    _html += '          </div>';
    _html += '      </div>';
    _html += '      <div class="info"></div>';
    _html +='<object type="application/x-fbframeworkplugin"  id="'+this._id +'" ></object>';
    _html += '      <div class="bottom" style="z-index: 999">';
    _html += '         <div id="listenBtn" class="bottom-btn listen-btn"  title=\'监听\'></div>';
    _html += '         <div id="talkBtn" class="bottom-btn talk-btn" title=\'对讲\'></div>';
    _html += '         <div id="fileBtn" class="bottom-btn file-btn" title=\'播放文件\'></div>';
    _html += '         <div id="addDeviceBtn" class="bottom-btn adddevice-btn" title=\'选择广播设备\'></div>';
    _html += '         <div id="realTalkBtn" class="bottom-btn realtalk-btn" title=\'分区喊话\'></div>';
    _html += '      </div>';
    _html += '      <div style="position:relative;height:1px;overflow:hidden;">';
    _html += '         <input type="file"  style="opacity:0;width:100%;height:1px;"/>';
    _html += '      </div>';
    _html += '  </div>';
    this._html = $(_html)[0];
    this._info = $('.info', this._html)[0];
    this._plugin = $('object', this._html)[0];
    this._title = $('.title', this._html);
    this._bottom = $('.bottom', this._html);


}

/**
 * 初始化可视对讲控件
 */
Monitor.prototype.initLoad = function() {
    var that = this;
    this._listenBtn = $('#listenBtn', this._html);
    this._talkBtn = $('#talkBtn', this._html);
    this._fileBtn = $('#fileBtn',this._html);
    this._realTalkBtn = $('#realTalkBtn',this._html);
    this._fileInput = $("input[type='file']",this._html);
    this._addDeviceBtn = $('#addDeviceBtn',this._html);
    this._info.listenstate = "stop";
    this._info.talkstate = "stop";
    this._info.defaultlisten = false; //存储是否登陆成功后开启监听
    this._info.logstate = "nologin";
    this._info.isfull = false;
    this._info.filestate = "";
    this._info.realtalk = "stop";
    if (this.getAttribute().filepath) {
        var filePath = this.getComponent().getAbsolutePath(this.getAttribute().filepath);
        this.filePath = filePath;
    }
    //是否隐藏按钮
    if (this.getAttribute().ishidebutton) {
        var ishidebutton = this.getComponent().getAbsolutePath(this.getAttribute().ishidebutton);
        this.ishidebutton = ishidebutton;
    }
    $(".full", this._html).bind("click", function() {
        that.full();
    });

    $(".nofull", this._html).bind("click", function() {
        that.nofull();
    });
    this._talkBtn.hide();
    this._listenBtn.bind("click", function() {
        that.clickListenBtn();
    });
    this._talkBtn.bind("click", function() {
        that.clickTalkBtn();
    });
    this._fileBtn.bind("click", function() {
        that.clickFileBtn();
    });
    this._realTalkBtn.bind("click", function() {
        that.clickRealTalkBtn();
    });
    this._addDeviceBtn.bind("click", function() {
        that.showDeviceList();
    });
    Monitor.global = this;
    this._monitorlist = Component.newInstance({"src":"/public.res/comp/monitorinfo/monitorlist/MonitorList.html",
        "id":"monitorlist","style":"display: none"});
    $(".monitor-body").append(this._monitorlist.getHTML());
}

/**
 * 显示对讲机设备选择对话框
 */
Monitor.prototype.showDeviceList = function() {
    //先将分区喊话与播放文件停止
    this.realPlayStop();
    this.filePlayStop();
    var that = this;
    //先默认查询设备
    var handleClass = this._monitorlist.getHandleClass();
    handleClass.showDefaultData();
    WinUtil.openDialog(this._monitorlist.getHTML(),{
        title:false,
        height:"auto",
        backdrop:"static"
    },function(){
        $(".in.modal").css("z-index","999999");
    },function(){
        if(handleClass._listenphonenum){
            that._info.reallistenphonenum = handleClass._listenphonenum;
        }else{
            that._info.reallistenphonenum = that._info.listenphonenum;
        }
    });
}

/**
 * 创建监听窗口并设置窗口大小
 */
Monitor.prototype.createPlayWnd = function() {
    var $html = $(this._html);
    var $plugin = $(this._plugin);
    var _width = $html.width();
    var _height = $html.height() - this._title.height() - this._bottom.height();
    if (!this.ishidebutton){
        $plugin.height(_height);
        $plugin.width(_width);
    }else{
        $plugin.height(1);
        $plugin.width(1);
    }
}

/**
 * 重设置窗口大小
 */
Monitor.prototype.resizePlayWnd = function() {
    var $html = $(this._html);
    var $plugin = $(this._plugin);
    var $title = $('.title', $html);
    var $bottom = $('.bottom', $html);
    var _width = $html.width();
    var _height = $html.height() - this._title.height() - this._bottom.height();
    $plugin.height(_height);
    $plugin.width(_width);
}

/**
 * 设置信息
 *
 * @param phoneNum 监听机器号 "11001"：11号主机1号分机
 * @param broadcastNums 广播机器号 "11001,11002" 多个机器号码用逗号分隔
 */
Monitor.prototype.setName = function(name) {
    $('.title', this._html).text(name);
}

/**
 * 登录，根据传入的startLister赖判断是否登录成功后开启监听
 * @param func 调用登陆成功后需要执行的方法
 */
Monitor.prototype.loginIn = function(func) {
    //判断当前的监听对讲是否已经登录，已经登录后不需要再进行登录
    if (this._info.logstate == "login" || this._info.logstate == "beginlogin") {
        return;
    }
    this._info.logstate = "beginlogin";
    var _soap = new HWSOAP("./monitor/getMonitorUserInfo.do");
    _soap.send({});
    var   _result = _soap.getResult().result["对讲参数"];
    var that = this;
    var paramsObj = {};
    paramsObj["sn_no"] = _result["sn号码"];
    paramsObj["host_no"] = _result["主机编号"];
    paramsObj["ip_str"] = _result["地址盒IP地址"];
    paramsObj["name_str"] = _result["用户名"];
    paramsObj["passwd_str"] = _result["口令"] ;
    var othis = this;
    pluginHander(this._plugin,"general_op","login_in",paramsObj,function(result){
        var flag = result.result;
        if(flag){
            othis._info.logstate = "login";
            if(func){//如果又回调方法，就执行回调
                func(result);
            }
        }else{
            othis._info.logstate = "nologin";
        }
    });
}

/**
 * 退出登录
 */
Monitor.prototype.loginOut = function() {
    pluginHander(this._plugin,"general_op","login_out",{},function(result){
    });
}

/**
 * 点击监听按钮
 */
Monitor.prototype.clickListenBtn = function() {
    if (this._info.listenstate == "stop") {
        this.listenStart();
    } else {
        this.listenStop();
    }
}

/**
 * 开始监听,先判断是否登陆成功，如果没有登陆成功，再进行登陆一次后执行开启监听操作
 */
Monitor.prototype.listenStart = function() {
    try {
        var othis = this;
        if (this._info.logstate == "login") {
            this._talkBtn.show();
            this._info.listenstate = "start";
            this._listenBtn.removeClass("listen-btn").addClass("listen-stop-btn");
            var paramsObj = {};
            paramsObj["term_id"] =  this._info.listenphonenum ;
            pluginHander(this._plugin,"listen_op","listen_start",paramsObj,function(result){

            });
        }else{
            this.loginIn(function(res){
                if (othis._info.logstate == "login") {
                    othis._talkBtn.show();
                    othis._info.listenstate = "start";
                    othis._listenBtn.removeClass("listen-btn").addClass("listen-stop-btn");
                    var paramsObj = {};
                    paramsObj["term_id"] =  this._info.listenphonenum ;
                    pluginHander(this._plugin,"listen_op","listen_start",paramsObj,function(result){

                    });
                }
            });
        }
    } catch (e) {

    }
}

/**
 * 停止监听
 */
Monitor.prototype.listenStop = function() {
    try {
        if (this._info.talkstate == "start") {
            this.talkStop();
            this._talkBtn.hide();
        }
        this._info.listenstate = "stop";
        this._listenBtn.removeClass("listen-stop-btn").addClass("listen-btn");
        var paramsObj = {};
        paramsObj["term_id"] =  this._info.listenphonenum;
        pluginHander(this._plugin,"listen_op","listen_stop",paramsObj,function(result){

        });
    } catch (e) {

    }
}

/**
 * 点击对讲按钮
 */
Monitor.prototype.clickTalkBtn = function() {
    if (this._info.talkstate == "stop") {
        this.talkStart();
    } else {
        this.talkStop();
    }
}

/**
 * 开启对讲，先判断是否已经登录，如果已经登录就直接开启监听，如果没有登录就先登录再开启对讲
 */
Monitor.prototype.talkStart = function() {
    try {
        var othis = this;
        if (this._info.logstate == "login") {
            this._info.talkstate = "start";
            this._talkBtn.removeClass("talk-btn").addClass("talk-stop-btn");
            pluginHander(this._plugin,"talk_op","talk_start",{},function(result){

            });
        }else{
            this.loginIn(function(res){
                if (othis._info.logstate == "login") {
                    othis._info.talkstate = "start";
                    othis._talkBtn.removeClass("talk-btn").addClass("talk-stop-btn");
                    pluginHander(othis._plugin,"talk_op","talk_start",{},function(result){

                    });
                }
            });
        }
    } catch (e) {

    }
}

/**
 * 停止对讲
 */
Monitor.prototype.talkStop = function() {
    try {
        this._info.talkstate = "stop";
        this._talkBtn.removeClass("talk-stop-btn").addClass("talk-btn");
        pluginHander(this._plugin,"talk_op","talk_stop",{},function(result){

        });
    } catch (e) {

    }
}

/**
 * 点击分区喊话
 */
Monitor.prototype.clickRealTalkBtn = function() {
    if (this._info.realtalk == "stop") {
        this.realPlayStart();
    } else {
        this.realPlayStop();
    }
}

/**
 * 对多个设备广播,参数3:固定
 */
Monitor.prototype.realPlayStart = function() {
    if(this._info.reallistenphonenum == undefined){
        alert("请先选择需要进行分区喊话的设备");
        return;
    }
    try {
        var othis = this;
        //关闭监听，监听在断开的状态时才可以开始喊话
        this.listenStop();
        //如果正在播放文件先暂停广播，广播和喊话不能同时进行
        if(this._info.filestate == "播放"){
            this.filePlayPause();
            this._info_isPause = true;//判断播放文件当前暂停是否是因为喊话而暂停
        }
        if (this._info.logstate == "login") {
            var paramsObj = {};
            paramsObj["term_list"] =  this._info.reallistenphonenum;
            pluginHander(this._plugin,"audio_broadcast_op","audio_broadcast_start",paramsObj,function(result){
                var flag = result.result;
                if(flag){
                    othis._info.realtalk = "start";
                    othis._realTalkBtn.removeClass("realtalk-btn").addClass("realtalk-stop-btn");
                }else{
                    alert("分区喊话失败");
                }
            });
        }
    } catch (e) {

    }
}
/**
 * 停止广播
 */
Monitor.prototype.realPlayStop = function() {
    try {
        if(this._info.reallistenphonenum == undefined){
            return;
        }
        var othis = this;
        var paramsObj = {};
        paramsObj["term_list"] =  this._info.reallistenphonenum ;
        pluginHander(this._plugin,"audio_broadcast_op","audio_broadcast_stop",paramsObj,function(result){
            var flag = result.result;
            if(flag){
                othis._info.realtalk = "stop";
                othis._realTalkBtn.removeClass("realtalk-stop-btn").addClass("realtalk-btn");
                if(othis._info.filestate == "暂停" && othis._info_isPause){
                    othis.filePlayResume();
                    othis._info_isPause = false;
                }
            }
        });
    } catch (e) {

    }
}

/**
 * 点击播放文件
 */
Monitor.prototype.clickFileBtn = function() {
    try {
        var that = this;
        if(this._info.filestate == "播放"){
            this.filePlayPause();
        }else if(this._info.filestate == "暂停"){
            this.filePlayResume();
        }else{
            if(this._info.logstate == "login"){
                this._fileInput.click()
                this._fileInput.bind("change",function(){
                    that.upload();
                });

            }
        }
    } catch (e) {

    }
}

/**
 * 添加音频
 * @param oControl
 */
Monitor.prototype.upload = function () {
    var urll = this._fileInput.val();
    if(!urll) return;
    //获取文件后缀名
    var path = urll.split(".");
    var suffix = path[path.length-1];
    if(suffix != "mp3" && suffix !="wav"){
        alert("只支持播放.mp3,.wav音频文件");
        return;
    }
    var that = this;
    this.uploadFile(function(oResult){
        var hurl = window.location.protocol+"//"+window.location.host+oResult.path;
        that.DownloadURLFile(hurl);
    })
}

/**
 * 上传文件
 */
Monitor.prototype.uploadFile = function(oCallBack){
    /* FormData 是表单数据类 */
    var fd = new FormData();
    fd.append("upload", 1);
    fd.append("upfile", this._fileInput.get(0).files[0]);
    $.ajax({
        url: "/broadcast/uploadVedio.do",
        type: "POST",
        processData: false,
        contentType: false,
        data: fd,
        success: function(oResult) {
            if(oResult && oResult.path){
                oCallBack(oResult);
            }
        }
    });
}


/**
 * 停止播放文件
 */
Monitor.prototype.filePlayStop = function() {
    try {
        var othis = this;
        pluginHander(this._plugin,"local_file_op","stop_play_local_file",{},
            function(result){
                var flag = result.result;
                if(flag){
                    othis._fileBtn.removeClass("file-pause-btn").addClass("file-btn");
                    othis._info.filestate = "";
                }
            });
    } catch (e) {

    }
}

/**
 * 暂停播放文件
 */
Monitor.prototype.filePlayPause = function() {
    try {
        var othis = this;
        pluginHander(this._plugin,"local_file_op","pause_play_local_file",{},
            function(result){
                var flag = result.result;
                if(flag){
                    othis._fileBtn.removeClass("file-pause-btn").addClass("file-btn");
                    othis._info.filestate = "暂停";
                }
            });
    } catch (e) {

    }
}

/**
 * 恢复播放文件
 */
Monitor.prototype.filePlayResume = function() {
    try {
        var othis = this;
        pluginHander(this._plugin,"local_file_op","resume_play_local_file",{},
            function(result){
                var flag = result.result;
                if(flag){
                    othis._fileBtn.removeClass("file-btn").addClass("file-pause-btn");
                    othis._info.filestate = "播放";
                }
            });
    } catch (e) {

    }
}

/**
 * 调节音量
 */
Monitor.prototype.sessionVol = function(vol) {
    try {
        var params = {};
        params["session_vol"] = vol;
        this._plugin.IPCast_SessionVol(this._info.filePlaynum, vol);
        pluginHander(this._plugin,"general_op","set_session_volume",params, function(result){});
    } catch (e) {

    }
}

/**
 * 从url下载文件,并开始播放
 */
Monitor.prototype.DownloadURLFile = function(url) {
    try {
        var othat = this;
        var params = {};
        params["file_url"] = url;
        params["term_list"] = this._info.listenphonenum;
        pluginHander(this._plugin,"remote_file_op","remote_file_start",params, function(result){
            var flag = result.result;
            if(flag){
                othat._info.filestate = "播放";
                othat._fileBtn.removeClass("file-btn").addClass("file-pause-btn");
            }

        });
    } catch (e) {
    }
}


/**
 * 设置播放信息,开始播放
 */
Monitor.prototype.setValue = function(deviceId) {

    //加载插件
    var paramsObj = {};
    paramsObj["plugin_name"] ="IOTP_FB_LonbonMonitor.dll";
    paramsObj["plugin_type_uuid"] ="9409c0c8-6e0c-4ad2-bdbc-86b7faae7940";
    paramsObj["net_url"] =this._id;
    paramsObj["local_dir"] =this._id;
    pluginHander( this._plugin,"framework","loadplugin",paramsObj,function(param){});

    this._deviceid = deviceId;
    if (this.isSetValue){
        return;
    }
    this.isSetValue = true;
    var _soap = new HWSOAP("./monitor/getMonitorDevice.do");
    _soap.send({
        "deviceId" : deviceId
    });
    var _result = _soap.getResult().result["返回结果"][0];
    this._deviceType = _result.类型编号;
    var _phoneNum = _result["分机"] != null ? _result["分机"]["主机编号"] + _result["分机号"] : null;
    var _name = _result["名称"];

    this._info.deviceType = this._deviceType;
    this._info.name = _name;
    this._info.listenphonenum = _phoneNum;
    if (_result['IP地址']!=null){
        this._info.ip = _result['IP地址'];
    }
    if (_result['用户名']!=null){
        this._info.username = _result['用户名'];
    }
    if (_result['口令']!=null){
        this._info.pass = _result['口令'];
    }
    this.setName(this._info.name);

    var othis = this;
    //登录成功后执行开启监听
    setTimeout(function(){
        othis.createPlayWnd();//设置窗口大小
        othis.loginIn(function(result){
            othis.listenStart();
        });
    },1000)




}
/**
 * 停止监控
 */
Monitor.listenStopByObj = function(obj) {
    var _plugin = $(obj).find("object")[0];
    var _info = $('.info', obj)[0];
    var _listenBtn = $('#listenBtn', obj);
    var _talkBtn = $('#talkBtn', obj);
    try {
        if (_info.talkstate == "start") {
            this.talkStop();
            _info.talkstate = "stop";
            _talkBtn.removeClass("talk-stop-btn").addClass("talk-btn");
        }
    } catch (e) {
        Monitor.log("停止对讲失败");
    }
    try {
        if (_info.listenstate == "start") {
            this.listenStop();
            _info.listenstate = "stop";
            _listenBtn.removeClass("listen-stop-btn").addClass("listen-btn");
        }
    } catch (e) {
        Monitor.log("停止监听失败");
    }
}

/**
 * 销毁插件
 */
Monitor.prototype.destroy = function() {
    try {
        this._html.removeChild(this._plugin);
        this._html.parentNode.removeChild(this._html);
    } catch (e) {
        Monitor.log("清空元素失败");
    }
}
/**
 * 销毁插件
 */
Monitor.destroyByObj = function(obj) {
    var _plugin = $(obj).find("object")[0];
    var _html = $(obj)[0];
    try {
        _html.removeChild(_plugin);
        _html.parentNode.removeChild(_html);
    } catch (e) {
        Monitor.log("清空元素失败");
    }
}


/**
 * 全屏方法
 */
Monitor.prototype.full = function() {
    this.getComponent().postMessage({
        id:"MSG_MONITOR_BEFOREFULL"
    });
    this.getComponent().postMessage({
        id:"MSG_3DMONITOR_BEFOREFULL"
    });//解决在3d地图上面不能全屏的问题
    //来绑插件改变窗口大小前需停止监听
    var oState = this._info.listenstate;
    if (this._info.listenstate == "start") {
        this.listenStop();
    }
    var _width = document.documentElement.clientWidth - 10;
    var _height = document.documentElement.clientHeight - 10;
    var _left = "5px";
    var _top = "5px";
    var _zindex = 9999;
    this.oldCss = {
        "position" : $(this._html).css('position'),
        "left" : $(this._html).css('left'),
        "top" : $(this._html).css('top'),
        "width" : $(this._html).css('width'),
        "height" : $(this._html).css('height'),
        "z-index" : 100
    };
    this.oldplugincss = {
        "width" : $(this._plugin).width(),
        "height" : $(this._plugin).height()
    };
    var _newcss = {
        "position":"fixed",
        "left":_left,
        "top":_top,
        "z-index":_zindex,
        "width" : _width,
        "height" : _height
    };
    var _height = _height - this._title.height() - this._bottom.height();
    try{
        $(this._html).css(_newcss);
        var that = this;
        setTimeout(function(){
            $(that._plugin).width(_width);
            $(that._plugin).height(_height);
            if (oState == "start") {
                that.listenStart();
            }
        },100);
        this._info.isfull = true;
        $(".nofull", this._html).show();
        $(".full", this._html).hide();
    }catch(e){
        $(this._html).css(this.oldCss);
        alert("全屏操作失败!");
        if(console) console.log("全屏失败:"+e);
    }
}

/**
 * 取消全屏
 */
Monitor.prototype.nofull = function() {
    var oState = this._info.listenstate;
    if (this._info.listenstate == "start") {
        this.listenStop();
    }
    var _html = $(this._html);
    var objplugin = $("object",_html);
    _html.css(this.oldCss);
    objplugin.height(this.oldplugincss.objheight);
    this._plugin.width = this.oldplugincss.width;
    this._plugin.height = this.oldplugincss.height;
    try {

        var that = this;
        setTimeout(function(){
            objplugin.height(that.oldplugincss.height);
            objplugin.width(that.oldplugincss.width);
            if (oState == "start") {
                that.listenStart();
            }
        },100);
        this._info.isfull = false;
        $(".full", _html).show();
        $(".nofull", _html).hide();
        this.getComponent().postMessage({
            id:"MSG_MONITOR_NOFULL"
        });
        this.getComponent().postMessage({
            id:"MSG_3DMONITOR_NOFULL"
        });//解决3d退出全屏后的操作
    } catch(e) {
    }
}

/**
 * 绑定页面销毁事件
 */
Monitor.bindWindowDestroy = function(_window) {
    $(_window).bind('beforeunload', function() {
        $("body").find(".monitor-body").each(function() {
            Monitor.destroyByObj(this);
        });
    });
}
/**
 * 只有中电兴发监听对讲有分组广播
 * @param port
 * @param filePath
 */
Monitor.prototype.openFileBroadcast = function(port, filePath) {
}
/**
 * 关闭播放文件
 * @param filePath
 * @param port
 */
Monitor.prototype.closeFileBroadcast = function(filePath, port) {
}
/**
 * 开门操作
 */
Monitor.prototype.openDoor = function() {
}
/**
 * 关门操作
 * @param filePath
 * @param port
 */
Monitor.prototype.closeDoor = function() {
}




